package com.example.shop.controller;

import com.example.shop.bean.CartBean;
import com.example.shop.bean.ProductBean;
import com.example.shop.bean.VxResp;
import com.example.shop.mapper.CartMapper;
import com.example.shop.mapper.CategoryMapper;
import com.example.shop.mapper.ProductMapper;
import com.example.shop.util.NotNullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController{
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CartMapper cartMapper;

    @ResponseBody
    @RequestMapping("/index/vx")
    public VxResp index(Integer cid){
        VxResp vx=new VxResp();
        vx.categorys=categoryMapper.selectList(null);
        vx.hots=productMapper.selectHot();
        if(cid==null&&!vx.categorys.isEmpty()){
            //cid没有，类别数组有类别
            cid=vx.categorys.get(0).id;
        }
        if(cid==null){
            //cid没有传，类别数组空
            vx.products=new ArrayList<>(); //返回空数组
        }else{//有类别，按照类别id查询这个类别的商品列表
            vx.products=productMapper.selectProduct(cid);
        }
        //时间格式化工具
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(ProductBean bean:vx.products){
            bean.ftime=sdf.format(bean.ctime);
        }
        return vx;
    }
    //  /product/list
    // selectList(null) 能够查询表中全部数据，得到的是一个List，List可以插入数据，删除数据
    //list.add(数组元素) list.remove(下标)
    @RequestMapping("/list")
    public String list(int uid ,HttpServletRequest req){  //selectList是 baseMapper内部的封装方法，sql
        req.setAttribute("retList",productMapper.selectList(uid));
        return "/product/list";
    }
    @RequestMapping("/del")
    public String del(int id){
        //删除
        int uid= productMapper.selectById(id).uid;
        productMapper.deleteById(id);
        return "redirect:/product/list?uid="+uid;
    }
    @GetMapping("/add")   //<a href="
    public String add(Integer id,HttpServletRequest req){
        req.setAttribute("cateList",categoryMapper.selectList(null));
        req.setAttribute("bean",id==null?null:productMapper.selectById(id));
        return "/product/add";
    }
    @PostMapping("/add")  //<form method ="post"
    public String add(ProductBean bean, HttpServletResponse resp){
        if(NotNullUtil.isBlank(bean)){
            //只要有一个空 就进来了
            return jsAlert("请完善信息!",resp);
        }
        if(bean.id==null){
            bean.ctime=new Date();
            productMapper.insert(bean);
        }else{
            productMapper.updateById(bean);
        }



        return "redirect:/product/list?uid="+bean.uid;
    }
    //get: <a>  重定向  网址  (更不安全）
    //post: <form method="post">   写代码方式访问  （比get安全)

    //绝对安全：对地址和参数加密  md5

    @RequestMapping("/all")
    public String all(HttpServletRequest req){ //查询视图中的data
        req.setAttribute("retList",productMapper.selectView());
        return "/product/all";
    }

    //进行商品查询
    @ResponseBody
    @RequestMapping("/search")
    public List<ProductBean> productSearch(String key){
        List<ProductBean> proL=productMapper.searchProduct(key);
        return proL;
   }
    //返回具体商品的数据
   @ResponseBody
    @RequestMapping("/select")
    public ProductBean productSel(Integer pid){
        ProductBean productBean;
        productBean=productMapper.selectById(pid);
        return productBean;
   }

}
